package core.proto;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.geom.Point2D;

import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import org.jdesktop.swingx.mapviewer.GeoPosition;
import org.jdesktop.swingx.mapviewer.Waypoint;

public class WaypointWithAnno extends Waypoint {
	
	private String onelineDisplay; 
	private String anno; 
	private JTextArea area; 	
	private final int MINWIDTH = 200;
	private final int MINHEIGHT = 100;
	private final int ONELINEWIDTH = 60; 
	private final int ONELINEHEIGHT = 20;
	private JPanel panel;
	private JScrollPane scroll;
	
	public WaypointWithAnno(GeoPosition pos){
		super(pos);
		panel = new JPanel(new BorderLayout());
		panel.setBorder(BorderFactory.createLineBorder(Color.black));
		scroll = new JScrollPane(panel);
		anno = "";
		area = new JTextArea();
		area.setLineWrap(true);
		area.setEditable(true);
		panel.add(area);
		
		
//		setTextAreaFocusListener();
	}
	
	// hide when mouse is away
	private void setTextAreaFocusListener(){
		area.setFocusable(true);
		area.addFocusListener(new FocusAdapter(){
			public void focusLost(FocusEvent fe){
				panel.setVisible(false);
			}
		});
	}
	
	public JPanel getPanel(){
		return panel;
	}
	
	public String getText() {
		return area.getText();
	}
	
	public void hideTextArea(){
		final int LEN = 10;
		anno = area.getText();
		area.setEditable(false);
		onelineDisplay = anno.length() <= LEN ? anno : anno.substring(0);
		area.setText(onelineDisplay);
		panel.setSize(ONELINEWIDTH, ONELINEHEIGHT);
		area.setBackground(Color.LIGHT_GRAY);
//		panel.setVisible(false);
	}
	
	public void showTextArea(int x, int y){
		area.setBackground(Color.white);;
		area.setText(anno);
		area.setEditable(true);
		panel.setSize(MINWIDTH, MINHEIGHT);
		panel.setLocation(new Point(x,y));
		panel.setVisible(true);
		area.requestFocusInWindow();
	}

	
}
